var refresh_console = function () {
    $.ajax({
        url : '/refresh-console',
        complete : function (jqXHR) {
            if (jqXHR.responseText.length > 0) {
                $('#last_console_row').replaceWith(jqXHR.responseText);
                $(".reservation_large_div").animate({scrollTop: $(".reservation_large_div").attr("scrollHeight")}, 1000);
            }

            setTimeout(function () {
                refresh_console();
            }, 5000);
        }
    });
};

function log_console_message(reservation_id, type, message) {
    $.ajax({
        type: "POST",
        url: '/console-log-message',
        data: {
            reservation_id: reservation_id,
            type: type,
            message: message
        },
        success: function () {
            $('#message_row_' + reservation_id).remove();
        }
    });
};

function set_restaurant(restaurant_id) {
    $.ajax({
        type: "POST",
        data: {restaurant_id: restaurant_id},
        url: '/set-restaurant',
        success: function () {
            location.reload();
        }
    });
}

function get_generic_modal_opt() {
    return {
        modal : true,
        draggable: true,
        width : "auto",
        height: "auto",
        position: 'center',
        stack: true,
        autoOpen : false,
        closeOnEscape: false,
        resizable: false,
        autoResize:true
    };
}

function set_datepicker(selector, future) {
    var year_range = "-0:+3";

    if (null != future && true == future)
        year_range = "-0:+3";

    $(selector).datepicker({showOn: 'button',
                             buttonImage: '/images/calendar_icon.gif',
                             buttonImageOnly: true,
                             changeMonth: true,
                             changeYear: true,
                             showWeek: true,
                             showOtherMonths: true,
                             selectOtherMonths: true,
                             minDate: 0,
                             yearRange: year_range});
}

function init_modal_dialog(response, options) {
    var dialog_content_selector = "#dialog_common";

    if (null != options) {
        if (null != options.destination_selector)
            dialog_content_selector = options.destination_selector;
    }

    $(dialog_content_selector).dialog("close");
    $(dialog_content_selector).html(response);
    $(dialog_content_selector).dialog($.merge(get_generic_modal_opt(), options.buttons));
    $(dialog_content_selector).dialog("open");

    if (null != options)
        if (null != options.datepicker)
            for (i = 0; i < options.datepicker.length; i++) {
                if (false == (options.datepicker[i] instanceof Array)) {
                    set_datepicker(options.datepicker[i]);
                }
                else {
                    set_datepicker(options.datepicker[i][0], true);
                }
            }
}

function show_group_menu_message() {
    if (0 != $('#group_size').length)
        $('#group_menu_message').html("Bij meer dan " + $('#group_size').val() + " personen moet u kiezen voor een menu.\nKlik op menu.");
    
    $('#group_menu_message').show();
}

function isValidNumberOfPersons(person_no) {
    if (isNaN(person_no))
        return false;

    return true;
}

function isGroupReservation(person_no) {
    if (0 == $('#group_size').length)
        return false;
    
    var group_size = $('#group_size').val();

    return (parseInt(person_no) >= parseInt(group_size));
}

/* check if it is a group reservation and if a menu has been selected */
function isValidGroupMenuContext(person_no) {
    if (!isValidNumberOfPersons(person_no)) {
        alert('Number of persons is invalid');
        
        return false;
    }

    if (isGroupReservation(person_no)) {
        if (!$("input[id^='reservation_backoffice_menu_id_']:checked").val()) {
            alert('You must choose a group menu');
            
            return false;
        }
    }

    return true;
}

/* check for block times */
function isValidBlockTime() {
    var date = getReservationDate();
    var time_hour = getReservationTimeHour();
    var time_minute = getReservationTimeMinute();
    
    if (null == date || null == time_hour || null == time_minute)
        return;
    
    $.ajax({
        type: "POST",
        url: '/check-time-block',
        dataType: 'json',
        data: {
            date: date,
            time_hour: time_hour,
            time_minute: time_minute
        },
        success: function (response) {
            if ("1" == response.error) {
                $('#block_time_reason').html(response.reason);
                $('#block_time_message').show();
            }
            else {
                $('#block_time_message').hide();
            }
        }
    });
}

function isSeatLimitReached() {
    var date = getReservationDate();
    var time_hour = getReservationTimeHour();
    var time_minute = getReservationTimeMinute();
    var person_no = getReservationPersonNo();
    
    if (null == date || null == time_hour || null == time_minute || null == person_no)
        return;
    
    $.ajax({
        type: "POST",
        url: '/check-seat-limit',
        dataType: 'json',
        data: {
            date: date,
            time_hour: time_hour,
            time_minute: time_minute,
            person_no: person_no
        },
        success: function (response) {
            if ("1" == response.error) {
                $('#seat_limit_message').show();
            }
            else {
                $('#seat_limit_message').hide();
            }
        }
    });
}

function getReservationDate() {
    if ($('#reservation_backoffice_date').length == 1)
        return $('#reservation_backoffice_date').val();

    if ($('#reservation_guest_date').length == 1)
        return $('#reservation_guest_date').val();
    
    return null;
}

function getReservationTimeHour() {
    if ($('#reservation_backoffice_time_hour').length == 1)
        return $('#reservation_backoffice_time_hour').val();

    if ($('#reservation_guest_time_hour').length == 1)
        return $('#reservation_guest_time_hour').val();
    
    return null;
}

function getReservationTimeMinute() {
    if ($('#reservation_backoffice_time_minute').length == 1)
        return $('#reservation_backoffice_time_minute').val();

    if ($('#reservation_guest_time_minute').length == 1)
        return $('#reservation_guest_time_minute').val();
    
    return null;
}

function getReservationPersonNo() {
    if ($('#reservation_backoffice_person_no').length == 1)
        return $('#reservation_backoffice_person_no').val();

    if ($('#reservation_guest_person_no').length == 1)    
        return $('#reservation_guest_person_no').val();
    
    return null;
}

function changeEmailTemplate() {
    $.ajax({
        type: "POST",
        url: '/settings/email-template/get',
        dataType: 'json',
        data: { category: $('#email_template_category option:selected').attr('id') },
        success: function (response) {
            $('#email_temaplate_body').val(response.body);
            $('#email_temaplate_subject').val(response.subject);
        }
    });
}